import 'dart:typed_data';
class ResponseApdu {
  
  Uint8List apdu;
  
  /// Constructs a ResponseAPDU from a byte array containing the complete
  /// APDU contents (conditional body and trailed).
  ///
  /// <p>Note that the byte array is cloned to protect against subsequent
  /// modification.
  ///
  /// @param apdu the complete response APDU
  /// @throws NullPointerException     if apdu is null
  /// @throws IllegalArgumentException if apdu.length is less than 2
  ResponseApdu(this.apdu){

    check(apdu);
    
  }

  void check(Uint8List apdu) {

    if (apdu.length < 2) {
      throw Exception('apdu must be at least 2 bytes long');
    }

  }

  ///Returns a copy of the data bytes in the response body. If this APDU as
  ///no body, this method returns a byte array with a length of zero.
  Uint8List getData() {

    Uint8List data = apdu.sublist(0,apdu.length-2);

    return data;
  }

  /// Returns the value of the status byte SW1 as a value between 0 and 255.
  int getSW1()
    => apdu[apdu.length - 2] & 0xff;

  /// Returns the value of the status byte SW2 as a value between 0 and 255.
  int getSW2()
    => apdu[apdu.length - 1] & 0xff;


  ///   Returns the value of the status bytes SW1 and SW2 as a single
  ///   status word SW.
  ///    It is defined as
  ///    <code>(getSW1() << 8) | getSW2()</code>.
  ///
  ///    @return the value of the status word SW.
  int getSW() {
    return (getSW1() << 8) | getSW2();
  }

  /// Returns a string representation of this response APDU.
  /// @return a String representation of this response APDU.
  @override
  String toString() {
    return "ResponseAPDU:  ${apdu.length}bytes, SW=${getSW()}";
  }

  ///
  bool isSuccess() {
    return getSW1() == 0x90 && getSW2() == 0x00;
  }

  ///
  bool isFailure() {
    return getSW1() == 0x63 && getSW2() == 0x00;
  }


}